package com.cow.net.tcp;




import com.cow.net.tcp.domain.DeviceValue;
import com.cow.net.tcp.domain.TypeData;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description:
 * Author:Giousa
 * Date:2017/2/9
 * Email:65489469@qq.com
 */
public abstract class CustomHeartbeatHandler extends ChannelInboundHandlerAdapter {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    protected String name;
    private int heartbeatCount = 0;

    public CustomHeartbeatHandler(String name) {
        this.name = name;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        DeviceValue.Request deviceValue = (DeviceValue.Request) msg;
        int type = deviceValue.getPackageType();

        logger.info("CustomHeartbeatHandler type="+type);

        switch (type){
            case 1:

                logger.info(name + " get ping msg from " + ctx.channel().remoteAddress());
                DeviceValue.Request.Builder deviceValuetmp  =DeviceValue.Request.newBuilder();


                deviceValuetmp.setPackageType(TypeData.PING); //数据数据类型
                deviceValuetmp.setDataType(deviceValue.getDataType()); //发动机类型
                deviceValuetmp.setContext("ok");
                deviceValuetmp.setCompanyId(deviceValue.getCompanyId());

                ctx.writeAndFlush(deviceValuetmp);
                break;

            case 3:
                handleData(ctx, msg);

                break;
        }

    }


    protected abstract void handleData(ChannelHandlerContext channelHandlerContext, Object msg);

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        // IdleStateHandler 所产生的 IdleStateEvent 的处理逻辑.
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            switch (e.state()) {
                case READER_IDLE:
                    handleReaderIdle(ctx);
                    break;
                case WRITER_IDLE:
                    handleWriterIdle(ctx);
                    break;
                case ALL_IDLE:
                    handleAllIdle(ctx);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        logger.info("---" + ctx.channel().remoteAddress() + " is active---");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {


        logger.info("---" + ctx.channel().remoteAddress() + " is inactive---");
    }

    protected void handleReaderIdle(ChannelHandlerContext ctx) {
        logger.info("---READER_IDLE---");
    }

    protected void handleWriterIdle(ChannelHandlerContext ctx) {
        logger.info("---WRITER_IDLE---");
    }

    protected void handleAllIdle(ChannelHandlerContext ctx) {
        logger.info("---ALL_IDLE---");
    }
}